package com.findviewbydream.evil.eda.View.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.findviewbydream.evil.eda.R;
import com.findviewbydream.evil.eda.Utils.Utils;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.View.Dialog
 * 时间：16-9-5 下午12:19
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：消息dialog
 ********************************************/
public class MessageDialog extends Dialog
{
    private static MessageDialog dialog;

    public static MessageDialog showDialog(Context context, String msg)
    {
        dialog = new MessageDialog(context, R.style.Dialog);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.message_dialog, null);

        final TextView textView = (TextView) layout.findViewById(R.id.message);
        final ImageView imageView = (ImageView) layout.findViewById(R.id.imageView);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        textView.setText(msg);


        ViewTreeObserver vto = textView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
        {
            @Override
            public void onGlobalLayout()
            {
                textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                ViewGroup.LayoutParams params = imageView.getLayoutParams();
                params.height = textView.getHeight() + 220;
                imageView.setLayoutParams(params);
            }
        });

        dialog.addContentView(layout, new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        dialog.setCanceledOnTouchOutside(true);

        //Glide.with(context).load(R.drawable.kraft).into(imageView);


        dialog.show();
        return dialog;
    }


    public static void dismissDialog()
    {
        if (dialog.isShowing())
        {
            dialog.dismiss();
            dialog = null;
        }
    }

    public MessageDialog(Context context)
    {
        super(context);
    }

    public MessageDialog(Context context, int themeResId)
    {
        super(context, themeResId);
    }

}
